package com.zhenwei.zscm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhenwei.zscm.entity.OrderEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
* @author DuckyYang
* @description 针对表【order】的数据库操作Mapper
* @createDate 2025-05-22 09:40:50
* @Entity com.zhenwei.zscm.entity.OrderEntity
*/
public interface IOrderMapper extends BaseMapper<OrderEntity> {
    @Select("SELECT order_status_name,count(1) as count FROM `order` WHERE deleted=0 GROUP BY order_status_name;")
    @Results(id = "map1", value = {
            @Result(property = "orderStatusName", column = "order_status_name"),
            @Result(property = "count", column = "count"),
    })
    List<Map<String, Object>> countByState();

    @Select("""
            SELECT B.repair_state, count(1) AS count
            FROM `order` AS A
            INNER JOIN order_product AS B ON B.deleted = 0 AND B.order_id = A.guid
            WHERE A.deleted = 0
            GROUP BY B.repair_state""")
    @Results(id = "map2", value = {
            @Result(property = "repairState", column = "repair_state"),
            @Result(property = "count", column = "count"),
    })
    List<Map<String, Object>> countByProductRepairState();

    @Select("""
            <script>
            SELECT A.guid, A.receive_number,A.receive_date,
                    B.product_name, B.product_sn, B.repair_state,
                    IFNULL(C.repair_site,'') AS repair_site,
                    IFNULL(D.return_date, '') AS return_date,
                    IFNULL(D.return_number, '') AS return_number
            FROM `order` AS A
            INNER JOIN order_product AS B ON B.deleted = 0 AND B.order_id = A.guid
            LEFT JOIN order_product_check AS C ON C.deleted = 0 AND C.order_id = A.guid AND C.product_sn = B.product_sn
            LEFT JOIN order_product_repair AS D ON D.deleted = 0 AND D.order_id = A.guid AND D.product_sn = B.product_sn
            WHERE A.deleted = 0 AND (B.product_sn IN
            <foreach item="item" collection="searchKey" open="(" close=")" separator=",">
                                           #{item}
                                       </foreach>
            OR A.receive_number IN
             <foreach item="item" collection="searchKey" open="(" close=")" separator=",">
                                           #{item}
                                       </foreach>)
            </script>
            """)
    @Results(id = "map3", value = {
            @Result(property = "guid", column = "guid"),
            @Result(property = "receiveNumber", column = "receive_number"),
            @Result(property = "receiveDate", column = "receive_date"),
            @Result(property = "productName", column = "product_name"),
            @Result(property = "productSn", column = "product_sn"),
            @Result(property = "repairState", column = "repair_state"),
            @Result(property = "repairSite", column = "repair_site"),
            @Result(property = "returnDate", column = "return_date"),
            @Result(property = "returnNumber", column = "return_number")
    })
    List<Map<String, Object>> customerSearch(@Param("searchKey") List<String> searchKey);
}




